package com.up.api.myimport;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

import java.text.MessageFormat;
import java.util.Map;

/**
 * @author: liuchengjun
 * @title: MyCacheSelector
 * @projectName: 海联技术中台V2.0
 * @depart 软件事业部
 * @description:
 * @date: 2024/1/31/031 9:26 (星期三)
 * @since V2.0
 */
public class MyCacheSelector implements ImportSelector {
    /**
     * 通过Import搭配ImportSelector完成有条件选择注入
     * @param annotationMetadata
     * @return
     */
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        //annotationMetadata源注解信息
        Map<String, Object> annotationAttributes = annotationMetadata.getAnnotationAttributes(Cache.class.getName());
        //通过 不同type注入不同的缓存到容器中
        CacheType type = (CacheType) annotationAttributes.get("type");
        switch (type) {
            case LOCAL: {
                return new String[]{LocalCacheService.class.getName()};
            }
            case REDIS: {
                return new String[]{RedisCachaService.class.getName()};
            }
            default: {
                throw new RuntimeException(MessageFormat.format("unsupport cache type {0}", type.toString()));
            }
        }
    }
}
